#!/bin/bash

# Common setup for all installation scripts
. /root/hack_hostname.sh
function echo_and_log {
    echo $1
    logger -- $1
}
function error_and_log {
    echo -e "\e[1;31m$1\e[0m"
    logger -- $1
}
function warning_and_log {
    echo -e "\e[1;33m$1\e[0m"
    logger -- $1
}

#Define source root directory
# if [ ! -n "$1" ] ;then
    SOURCE_ROOT="/vagrant"
# else
#    SOURCE_ROOT=$1
#fi

HOST_ENV=${HOSTNAME#*.}
PC_ENV=`echo ${HOSTNAME}|awk -F '.' '{print $2}'`

WEB_DIR=/var/lib/foxit_ucx

# Figuring out our download server
DL_SERVER_FILE_NAME=/tmp/download_server.txt

for i in {1..3}
do
if [ ! -e $DL_SERVER_FILE_NAME ] || [ `find "$DL_SERVER_FILE_NAME" -mmin +1440` ]; then
    echo "Download server info outdated, fetching..."
    sudo curl -s -k -o $DL_SERVER_FILE_NAME https://cloud.connectedpdf.com/get-download-server.php && sudo chmod 777 $DL_SERVER_FILE_NAME
fi
done
if [ ! -e $DL_SERVER_FILE_NAME ]; then
    echo "Check your network, can not get the url:https://cloud.connectedpdf.com/get-download-server.php."
    exit 1
fi
DL_SERVER=`cat $DL_SERVER_FILE_NAME`
if [ "$DL_SERVER" = "release-hf.sumilux.com" ]; then
    LOCATION='hf'
elif [ "$DL_SERVER" = "it-deploy.foxitcloud.com" ]; then
    LOCATION='fz'
else
    LOCATION='us'
fi
#ping_time=`ping -c 4 $DL_SERVER | grep rtt | awk -F '/' '{print$(5)}'`
#ping_time=${ping_time%.*}
#if [ $ping_time -gt 100 ]; then
#    warning_and_log "Connect server time is too long. Maybe you connected the VPN, Time: $ping_time"
#fi

# echo Now using download server $DL_SERVER ...

# Setting YUM architecture "i386" or "x86_64" for later repo operations
YUM_BASEARCH=`uname -i`
YUM_ARCH=`uname -m`
YUM_SYS=`cat /etc/redhat-release |awk -F" " '{print$(NF-1)}'`

ID_RSA_PATH=/root/svcconfig/id_rsa/id_rsa
#sudo chmod 600 ${ID_RSA_PATH}

RSH_OPTS="ssh -p 333 -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -i ${ID_RSA_PATH}"
RSYNC_OPTS="-avc --delete" # TODO: add --info progress with a newer version of RSYNC
